package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pilipili.common.R;
import com.example.pilipili.dao.AdminMapper;
import com.example.pilipili.pojo.entity.Admin;
import com.example.pilipili.service.AdminService;

import com.example.pilipili.utils.MD5U;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class AdminServiceImpl extends  ServiceImpl<AdminMapper, Admin>  implements AdminService {


    @Autowired
    AdminMapper adminMapper ;
    @Override
    public R<Admin> add(Admin entry) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("name",entry.getName()).eq("phone",entry.getPhone()) ;
        if(adminMapper.exists(qw)) return R.failed("该用户已经存在") ;

        // MD5加密
        entry.setPassword(MD5U.getMD5Password(entry.getPassword()));
        entry.setTime(LocalDate.now());
        adminMapper.insert(entry) ;
        entry.setPassword(null);
        return R.ok();
    }
}
